var Customer = {
		validateName : function(){
	        var result = false;
	        var value = $.trim($("#username").val());
            ValidationUtil.clearErrorMsg("username");
            if(ValidationUtil.isBlank(value)){
                ValidationUtil.showErrorMsg("username", "nameEmpty");
                return result;
            }
            var baseUrl = $('#baseurl').val();
            var id = $('#id').val();
           
            $.ajax({
                type: 'post', 
                url: baseUrl+'/customer/customer/checknameunique', 
                data: {"name" : value, "id" : id}, 
                async: false,  //false is synchronization , true is asynchronism
                dataType : 'json',
                success: function(data){ 
                   if(data[0]=='true'){
                	   ValidationUtil.clearErrorMsg("username");
                       result = true; 
                   }else{
                	   ValidationUtil.showErrorMsg("username", "nameExist");
                	   result = false; 
                   }
                }
                 
               });
           return result;
        },
        
        validateEmail : function(){
        	var result = true;
	        var value = $.trim($("#emailbox").val());
            ValidationUtil.clearErrorMsg("emailbox");
            if(ValidationUtil.isBlank(value)){
                ValidationUtil.showErrorMsg("emailbox", "emailEmpty");
                result = false;
            } else if(!ValidationUtil.isEmail(value)){
            	ValidationUtil.showErrorMsg("emailbox", "emailErr");
            	result = false;
            }
            if(result == false){
            	return result;
            }
            var baseUrl = $('#baseurl').val();
            var id = $('#id').val();
            $.ajax({
                type: 'post', 
                url: baseUrl+'/customer/customer/checkemailunique', 
                data: {"email" : value, "id" : id}, 
                async: false,  //false is synchronization , true is asynchronism
                dataType : 'json',
                success: function(data){ 
                   if(data[1]!='true'){
                	   ValidationUtil.showErrorMsg("emailbox", "emailExist");
                	   result = false; 
                   }
                }
                 
               });
           return result;
        },
        validatePsw : function(){
	        var password = $.trim($("#password").val());
            ValidationUtil.clearErrorMsg("password");
            if(ValidationUtil.isBlank(password)){
                ValidationUtil.showErrorMsg("password", "pswEmpty");
                return false;
            } else if(password.length<6 || password.length>18){
            	ValidationUtil.showErrorMsg("password", "pswErr");
            	return false;
            }
                return true;
        },
		
		validateTeamName : function(){
        	var value = $.trim($("#teamName").val());
            ValidationUtil.clearErrorMsg("teamName");
            if(ValidationUtil.isBlank(value)){
                ValidationUtil.showErrorMsg("teamName", "teamNameEmpty");
                return false;
            }
                return true;
        },

        validateFullName : function(){
        	var value = $.trim($("#fullName").val());
            ValidationUtil.clearErrorMsg("fullName");
            if(ValidationUtil.isBlank(value)){
                ValidationUtil.showErrorMsg("fullName", "fullNameEmpty");
                return false;
            }
                return true;
        },

      validateProvince : function(){
        	var value = $.trim($("#province").val());
            ValidationUtil.clearErrorMsg("province");
            if(ValidationUtil.isBlank(value)){
                ValidationUtil.showErrorMsg("province", "provinceEmpty");
                return false;
            }
                return true;
      },

      validateCity : function(){
    	  var value = $.trim($("#city").val());
          ValidationUtil.clearErrorMsg("city");
          if(ValidationUtil.isBlank(value)){
              ValidationUtil.showErrorMsg("city", "cityEmpty");
              return false;
          }
              return true;
      },

     validateAddress : function(){
    	  var value = $.trim($("#address").val());
          ValidationUtil.clearErrorMsg("address");
          if(ValidationUtil.isBlank(value)){
              ValidationUtil.showErrorMsg("address", "addrEmpty");
              return false;
          }
              return true;
     },

    validateZipCode : function(){
    	 var value = $.trim($("#zipCode").val());
         ValidationUtil.clearErrorMsg("zipCode");
         if(ValidationUtil.isBlank(value)){
             ValidationUtil.showErrorMsg("zipCode", "zipEmpty");
             return false;
         }
             return true;
    },
    
    validateMobile : function(){
   	    var value = $.trim($("#mobile").val());
        ValidationUtil.clearErrorMsg("mobile");
        if(ValidationUtil.isBlank(value)){
            ValidationUtil.showErrorMsg("mobile", "mobileEmpty");
            return false;
        } else if(!ValidationUtil.isInteger(value) || value.length!=11){
        	ValidationUtil.showErrorMsg("mobile", "mobileErr");
        	return false;
        }
            return true;
   },
   
   validatePointsBuyer : function(){
   	var value = $.trim($("#pointsBuyer").val());
       ValidationUtil.clearErrorMsg("pointsBuyer");
       if(!ValidationUtil.isBlank(value) && !ValidationUtil.isInteger(value) && value!=0){
           ValidationUtil.showErrorMsg("pointsBuyer", "pointsBuyerErr");
           return false;
       }
       return true;
   },
   
   validatePointsSeller : function(){
	   	var value = $.trim($("#pointsSeller").val());
	       ValidationUtil.clearErrorMsg("pointsSeller");
	       if(!ValidationUtil.isBlank(value) && !ValidationUtil.isInteger(value) && value!=0){
	           ValidationUtil.showErrorMsg("pointsSeller", "pointsSellerErr");
	           return false;
	       }
	       return true;
   },
   
   validatePointsBuyerTotal : function(){
	   	var value = $.trim($("#pointsBuyerTotal").val());
	       ValidationUtil.clearErrorMsg("pointsBuyerTotal");
	       if(!ValidationUtil.isBlank(value) && !ValidationUtil.isInteger(value) && value!=0){
	           ValidationUtil.showErrorMsg("pointsBuyerTotal", "pointsBuyerTotalErr");
	           return false;
	       }
	       return true;
	   },
	   
    validatePointsSellerTotal : function(){
		var value = $.trim($("#pointsSellerTotal").val());
		ValidationUtil.clearErrorMsg("pointsSellerTotal");
		if(!ValidationUtil.isBlank(value) && !ValidationUtil.isInteger(value) && value!=0){
		     ValidationUtil.showErrorMsg("pointsSellerTotal", "pointsSellerTotalErr");
		     return false;
		}
		return true;
	},

	   
	   

    openAddDialog: function(baseUrl){        
        Common.showDialog({
            url : baseUrl ,
            height : 450 ,
            width : 600 ,
            dataDiv : "customerDialog"
        });
    },

    openEditDialog: function(url){
        Common.showDialog({
            url : url ,
            height : 450 ,
            width : 750 ,
            dataDiv : "customerDialog"
        });
    },

    addCustomer: function(baseUrl){
    	var result = true;
        if(!this.validateName()){
            result = false;
        }
        if(!this.validateEmail()){
            result = false;
        }
        if(!this.validatePsw()){
            result = false;
        }
       
        var userType = $.trim($("#userType").val());
        if(userType==3 || userType==5){
        	if(!this.validateTeamName()){
                result = false;
            }
        	if(!this.validateFullName()){
                result = false;
            }
        	if(!this.validateProvince()){
                result = false;
            }
        	if(!this.validateCity()){
                result = false;
            }
        	if(!this.validateAddress()){
                result = false;
            }
        	if(!this.validateZipCode()){
                result = false;
            }
        	if(!this.validateMobile()){
                result = false;
            }
        }
        if(result){
        var url = baseUrl + "/customer/customer/subadd";
        var data = $("#customerForm").serialize();
            $.ajax({
                type:"POST",
                async:false,
                url:url,
                data: data,
                dataType:"Html",
                success:function(responseText){
                    if(responseText != "true"){
                        $("#customerDialog").empty();
                        $("#customerDialog").html(responseText);
                    }else{
                        Common.closeDialog('customerDialog');
                        var ajaxUrl = $("#reloadUrl").val();
                        DBGrid.ajaxSearch(ajaxUrl);
                    }
                }
           });
        }
    },

    editCustomer: function(baseUrl){
    	var result = true;
    	
        if(!this.validateName()){
            result = false;
        }
        
        if(!this.validateEmail()){
        	result = false;
        }
        
        var password = $.trim($("#password").val());
        if(password.length>0 && (password.length<6 || password.length>18)){
        	ValidationUtil.showErrorMsg("password", "pswErr");
        	result = false;
        }
        if(!this.validatePointsBuyer()){
            result = false;
        }
        if(!this.validatePointsSeller()){
            result = false;
        }
        
        if(!this.validatePointsBuyerTotal()){
            result = false;
        }
        if(!this.validatePointsSellerTotal()){
            result = false;
        }
        var userType = $.trim($("#userType").val());
        if(userType==3 || userType==5){
        	if(!this.validateTeamName()){
                result = false;
            }
        	if(!this.validateFullName()){
                result = false;
            }
        	if(!this.validateProvince()){
                result = false;
            }
        	if(!this.validateCity()){
                result = false;
            }
        	if(!this.validateAddress()){
                result = false;
            }
        	if(!this.validateZipCode()){
                result = false;
            }
        	if(!this.validateMobile()){
                result = false;
            }
        }
        var editPromptInfo = $('#editPromptInfo').val();
        
        if(result){
          if(confirm(editPromptInfo)){
            var url = baseUrl + "/customer/customer/subedit";
            var data = $("#customerForm").serialize();
            $.ajax({
                type:"POST",
                async:false,
                url:url,
                data: data,
                dataType:"Html",
                success:function(responseText){
                    if(responseText != "true"){
                       $("#customerDialog").empty();
                       $("#customerDialog").html(responseText);
                    }else{
                       Common.closeDialog('customerDialog');
                       var ajaxUrl = $("#reloadUrl").val();
                       DBGrid.ajaxSearch(ajaxUrl);
                    }
                }
            });
          }
        }
     },

    deleteCustomer: function(url){
    	var delPromptInfo = $('#delPromptInfo').val();
    	 
        if(confirm(delPromptInfo)){
            $.ajax({
                type:"GET",
                async:false,
                url:url,
                dataType:"Html",
                success:function(responseText){
                    Common.closeDialog('customerDialog');
                    var ajaxUrl = $("#reloadUrl").val();
                    DBGrid.ajaxSearch(ajaxUrl);
                }
            });
        }
    },

    search:  function(){
        $("#advanceSearchStatus").val(0);
        Common.submit();
    },

    advanceSearch:  function(){
        $("#advanceSearchStatus").val(1);
        Common.submit();
    },
    
    changeType : function(){
    	var iniUserType = $('#iniUserType').val();
    	var userType = $('#userType').val();
    	if(iniUserType == 3){
    		if(userType!=3 && userType!=1){
    		    $('#userType').val(3);
    		    jAlert($('#userTypeChangeInfo1').val());
    		} else if(userType == 1) {
    			var baseUrl = $('#baseurl').val();
    			url = baseUrl+'/customer/customer/checkagency';
    			var id = $('#id').val();
    			if(!this.checkagency(url,id)){
    				$('#userType').val(3);
        		    jAlert($('#userTypeChangeInfo2').val());
    			}
    			
    		}
    	}
    	if(userType==1 || userType==6 || userType==7){
    		$('#receiveAddrInfo').hide();
    	}else if(userType==3 || userType==5){
    		$('#receiveAddrInfo').show();
    	} else {
    		$('#receiveAddrInfo').show();
    	}
    },
    
    checkagency : function(url,userId){
    	var result = true;
    	$.ajax({
            type:"POST",
            async:false,
            data:{'userId': userId },
            url:url,
            dataType:"json",
            success:function(data){
                if(data == 1){
                	result = false;
                }
            }
        });
    	return result;
    },
    
    paginateWishlist : function(baseUrl,curPage,customerId){
    	$.ajax({
            type:"POST",
            async:false,
            data:{'curPage': curPage, "customerId":customerId },
            url:baseUrl + '/customer/customer/paginatewishlist',
            dataType:"html",
            success:function(data){
                $('#wishlist').html(data);
            }
        });
    }
    	
}